package com.huizluo.module4.student;

import java.util.HashMap;

public class AddCommand extends Command {

    private Store database;

    public AddCommand(String commandName, String commandDesc, Store database) {
        super(commandName,commandDesc);
        this.database = database;
    }

    public void notise(){
        System.out.println("请输入学生信: id=xx name=xx age=xx");
    }

    @Override
    public void run(HashMap<String, String> args) throws Exception {
        int id = Integer.parseInt(args.get("id"));
        if (id<=0){
            throw new NumException();
        }
        String name = args.get("name");
        if (name == null){
            throw new Exception("名称不可为空");
        }
        int age = Integer.parseInt(args.get("age"));
        if (age < 0 || age > 150){
            throw new AgeException();
        }

        Student s = new Student(id,name);
        s.setAge(age);
        Item item = new Item();
        item.ID = s.getID();
        item.obj = s;
        this.database.Add(item);
        System.out.println("操作成功");

    }
}
